package com.lianrui.web.spring;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 *功能描述：与Servlet相关的工具类方法
 *
 * @author LJQ
 *
 * 2015年12月17日 下午2:41:56
 */
public class ServletUtils {

	public static void printSession() {
		HttpSession session = getCurrentSession();
		for (Enumeration<?> e = session.getAttributeNames(); e.hasMoreElements();) {
			String key = (String) e.nextElement();
			Object val = session.getAttribute(key);
			System.out.println(val);
		}
	}
	
	/**
	 * 功能描述：获得当前session对象
	 * @return 当前session对象
	 */
	public static HttpSession getCurrentSession() {
		HttpSession session = getCurrentRequest().getSession();
		return session;
	}
	
	/**
	 * 功能描述：获得当前request对象
	 * @return 当前request对象
	 */
	public static HttpServletRequest getCurrentRequest() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}
	
	/**
	 * 功能描述：获得当前response对象
	 * @return 当前response对象
	 */
	public static HttpServletResponse getCurrentResponse() {
		HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
		return response;
	}
}
